import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { getUserAndMenuPermission } from '@/api/common/common'


export const useUserStore = defineStore('user', {
    state: () => {
        return {
            token: undefined,
            isLoadInfo: false,
            id: undefined,
            username: undefined,
            deptId: undefined,
            menus: [],
            permissionCodes: [],
        }
    },
    // getters:{
    //     getToken(){
    //         return this.token
    //     }
    // },
    actions: {
        setToken(token) {
            this.token = token
        },
        async loadInfo() {
            await getUserAndMenuPermission().then(data => {
                this.setId(data.id)
                this.setUsername(data.name)
                this.setDeptId(data.deptId)
                this.setMenus(data.menus)
                this.setPermissionCodes(data.permissionCodes)
                this.isLoadInfo = true
            })
            return true
        },
        loginout() {
            this.token = undefined
            this.id = undefined
            this.username = undefined
            this.deptId = undefined
            this.menus = []
            this.isLoadInfo = false
        },
        setId(id) {
            this.id = id
        },
        setUsername(username) {
            this.username = username
        },
        setMenus(menus) {
            this.menus = menus
        },
        setDeptId(deptId) {
            this.deptId = deptId
        },
        setPermissionCodes(permissionCodes) {
            this.permissionCodes = permissionCodes
        },
    }
})
